$(document).ready(function() {
    function checkUserName() {
        var uCode = $("#regModal #regForm #uCode"),
            uCodeTip = $("#regModal #regForm #uCodeTip");
        uCode.on({
            focus: function() {
                uCodeTip.attr("class", "col-md-4").css("color", "#4876FF").html("用户名由字母或数字组成")
            },
            blur: function() {
                var uCode = $(this).val(),
                    c = /^[A-Za-z0-9]{4,}$/;
                c.test(uCode) ? $.ajax({
                    method: "GET",
                    url: "userCodeCheck?userCode=" + uCode,
                    success: function(uCode) {
                        uCode.isRegistered >= 1 ? uCodeTip.attr("class", "col-md-4").css("color", "#EE2C2C").html("用户名已存在") : uCodeTip.attr("class", "col-md-4 glyphicon glyphicon-ok").css("color", "#00EE00").html("")
                    }
                }) : 0 == uCode.length ? uCodeTip.attr("class", "col-md-4").css("color", "#EE2C2C").html("用户名不能为空") : uCodeTip.attr("class", "col-md-4").css("color", "#EE2C2C").html("用户名格式有误")
            }
        })
    }
    function checkPassword() {
        uPwd.on({
            focus: function() {
                uPwdTip.attr("class", "col-md-4").css("color", "#4876FF").html("密码由字母或数字组成")
            },
            blur: function() {
                var o = uPwd.val(),
                    l = /^[A-Za-z0-9]{4,}$/;
                l.test(o) ? uPwdTip.attr("class", "col-md-4 glyphicon glyphicon-ok").css("color", "#00EE00").html("") : 0 == o.length ? uPwdTip.attr("class", "col-md-4").css("color", "#EE2C2C").html("密码不能为空") : uPwdTip.attr("class", "col-md-4").css("color", "#EE2C2C").html("密码格式有误")
            }
        }),
            uPwd2.on({
                blur: function() {
                    var o = uPwd.val(),
                        l = uPwd2.val();
                    0 == l.length ? uPwd2Tip.attr("class", "col-md-4").css("color", "#EE2C2C").html("密码不能为空") : o != l ? uPwd2Tip.attr("class", "col-md-4").css("color", "#EE2C2C").html("两次密码输入不一致") : uPwd2Tip.attr("class", "col-md-4 glyphicon glyphicon-ok").css("color", "#00EE00").html("")
                }
            })
    }
    function getData(o) {
        $.ajax({
            type: "POST",
            url: "register",
            data: JSON.stringify(o),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function() {
                $("#regModal").modal("hide"),
                    alert("注册成功！"),
                    window.location.href("index")
            }
        })
    }
    var regForm = $("#regModal #regForm"),
        uCode = $("#regForm #uCode"),
        uName = $("#regForm #uName"),
        uPwd = $("#regForm #uPwd"),
        uPwd2 = $("#regForm #uPwd2"),
        uEmail = $("#regForm #uEmail"),
        uCodeTip = $("#regModal #regForm #uCodeTip"),
        uPwdTip = $("#regModal #regForm #uPwdTip"),
        uPwd2Tip = $("#regModal #regForm #uPwd2Tip");
    checkUserName(),
        checkPassword(),
        $("#regForm #regSubmit").on({
            click: function() {
                var uCodeVal = uCode.val(),
                    uNameVal = uName.val(),
                    regForm = uEmail.val(),
                    uPwd2 = uPwd.val(),
                    u = {
                        userCode: uCodeVal,
                        userPassword: uPwd2,
                        userName: uNameVal,
                        email: regForm
                    };
                uCodeTip.hasClass("glyphicon-ok") && uPwdTip.hasClass("glyphicon-ok") && uPwd2Tip.hasClass("glyphicon-ok") && getData(u)
            }
        }),
        $("#regModal .close, #regModal #regQuit").on({
            click: function() {
                regForm[0].reset(),
                    uCodeTip.attr("class", "").html(""),
                    uPwdTip.attr("class", "").html(""),
                    uPwd2Tip.attr("class", "").html("")
            }
        })
});